import 'package:epidemic_data/data/data.dart';
import 'package:epidemic_data/http/http_util.dart';
import 'package:epidemic_data/model/ncov_response.dart';
import 'package:flutter/material.dart';

class StatsGrid extends StatefulWidget {
  int index;

  StatsGrid(this.index);

  @override
  _StatsGridState createState() {
    return _StatsGridState();
  }
}

class _StatsGridState extends State<StatsGrid> {

  int confirmedCount = 0;
  int suspectedCount = 0;
  int curedCount = 0;
  int deadCount = 0;
  int seriousCount = 0;

  int foreignConfirmedCount = 0;
  int foreignSuspectedCount = 0;
  int foreignCuredCount = 0;
  int foreignDeadCount = 0;


  @override
  void initState() {
    super.initState();
    getData();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: MediaQuery
          .of(context)
          .size
          .height * 0.25,
      child: Column(
        children: <Widget>[
          Flexible(
              child: Row(
                children: <Widget>[
                  _buildStatsCard('总确诊数', widget.index==0?confirmedCount:foreignConfirmedCount, Colors.orange),
                  _buildStatsCard('总死亡数', widget.index==0?deadCount:foreignDeadCount, Colors.red),
                ],
              )),
          Flexible(
              child: Row(
                children: <Widget>[
                  _buildStatsCard('总康复数', widget.index==0?curedCount:foreignCuredCount, Colors.green),
                  _buildStatsCard('境外输入', widget.index==0?seriousCount:0, Colors.lightBlue),
                  _buildStatsCard('疑似病例', widget.index==0?suspectedCount:foreignSuspectedCount, Colors.purple),
                ],
              )),
        ],
      ),
    );
  }

  Expanded  _buildStatsCard(String title, int count, MaterialColor color){
    return Expanded(
      child: Container(
        margin: const EdgeInsets.all(8.0),
        padding: const EdgeInsets.all(10.0),
        decoration: BoxDecoration(
          color: color,
          borderRadius: BorderRadius.circular(10.0),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
              title,
              style: const TextStyle(
                color: Colors.white,
                fontSize: 15.0,
                fontWeight: FontWeight.w600,
              ),
            ),
            Text(
              count.toString(),
              style: const TextStyle(
                color: Colors.white,
                fontSize: 18.0,
                fontWeight: FontWeight.bold,
              ),
            ),
          ],
        ),
      ),
    );
  }

  //获取网络数据
  void getData() {
    // HttpUtil.get("http://api.tianapi.com/ncov/index", (data) {
    //   NcovResponse response = NcovResponse.fromJson(data);
    //   setState(() {
    //     confirmedCount = response.newslist[0].desc.confirmedCount;
    //     suspectedCount = response.newslist[0].desc.suspectedCount;
    //     curedCount = response.newslist[0].desc.curedCount;
    //     deadCount = response.newslist[0].desc.deadCount;
    //     seriousCount = response.newslist[0].desc.seriousCount;
    //
    //     foreignConfirmedCount = response.newslist[0].desc.foreignStatistics.currentConfirmedCount;
    //     foreignDeadCount = response.newslist[0].desc.foreignStatistics.deadCount;
    //     foreignSuspectedCount = response.newslist[0].desc.foreignStatistics.suspectedCount;
    //     foreignCuredCount = response.newslist[0].desc.foreignStatistics.curedCount;
    //   });
    // },params: {"key":"132133af3d8bdfb0c50d265159e15983"}, errorCallBack: (errorMsg) {
    //   print("error:" + errorMsg);

      NcovResponse response = NcovResponse.fromJson(nationalInfoFake);
      setState(() {
        confirmedCount = response.newslist[0].desc.confirmedCount;
        suspectedCount = response.newslist[0].desc.suspectedCount;
        curedCount = response.newslist[0].desc.curedCount;
        deadCount = response.newslist[0].desc.deadCount;
        seriousCount = response.newslist[0].desc.seriousCount;

        foreignConfirmedCount = response.newslist[0].desc.foreignStatistics.currentConfirmedCount;
        foreignDeadCount = response.newslist[0].desc.foreignStatistics.deadCount;
        foreignSuspectedCount = response.newslist[0].desc.foreignStatistics.suspectedCount;
        foreignCuredCount = response.newslist[0].desc.foreignStatistics.curedCount;
      });
  }
}